package com.person.jiaiplan.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.person.jiaiplan.contant.PlanRulesEnum;
import com.person.jiaiplan.mapper.SchedulePlanLogsMapper;
import com.person.jiaiplan.pojo.dto.AddSchedulePlanReqDTO;
import com.person.jiaiplan.pojo.entity.SchedulePlanLogs;
import com.person.jiaiplan.pojo.signle.SignleSnowFlake;
import com.person.jiaiplan.service.SchedulePlanLogsService;
import com.person.jiaiplan.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

@Slf4j
@Service
public class SchedulePlanLogsServiceImpl extends ServiceImpl<SchedulePlanLogsMapper, SchedulePlanLogs> implements SchedulePlanLogsService {

    @Override
    public Boolean add(AddSchedulePlanReqDTO addSchedulePlanReqDTO) {
        if (StrUtil.isBlank(addSchedulePlanReqDTO.getScheduleId())) {
            throw new RuntimeException("计划Id不能为空");
        }
        if (StrUtil.isBlank(addSchedulePlanReqDTO.getRuleValues())){
            throw new RuntimeException("计划规则值不能为空");
        }
        Boolean result = true;
        Date startTime = addSchedulePlanReqDTO.getPlanStartTime();
        Date endTime = addSchedulePlanReqDTO.getPlanFinishTime();
        Integer planRules = addSchedulePlanReqDTO.getPlanRules();

        if (PlanRulesEnum.WEEK.getCode().equals(planRules)) {
            // 用数组接收 ruleValues
            String[] ruleValues = addSchedulePlanReqDTO.getRuleValues().split(",");
            // 根据日期数据获取接下来到执行时间结束的所有日期
            String[] planDates = DateUtils.getPlanDate(startTime, endTime, ruleValues);
            result = createOrUpdatePlanLogs(planDates, addSchedulePlanReqDTO);

        } else if (PlanRulesEnum.INTERVALS.getCode().equals(planRules)) {
            // 用数组接收 ruleValues
            // 根据日期数据获取接下来到执行时间结束的所有日期
            String[] planDates = DateUtils.getPlanDateByIntervals(startTime, endTime, Integer.valueOf(addSchedulePlanReqDTO.getRuleValues()));
            result = createOrUpdatePlanLogs(planDates, addSchedulePlanReqDTO);
        } else {
            // 用数组接收 ruleValues
            String[] ruleValues = addSchedulePlanReqDTO.getRuleValues().split(",");
            // 根据日期数据获取接下来到执行时间结束的所有日期
            result = createOrUpdatePlanLogs(ruleValues, addSchedulePlanReqDTO);
        }
        log.info("add success");
        return result;
    }

    @Override
    public Boolean removeByScheduleId(Long ScheduleId) {
        return this.remove(Wrappers.<SchedulePlanLogs>lambdaQuery().eq(SchedulePlanLogs::getScheduleId, ScheduleId));
    }


    /**
     * 创建计划日期
     *
     * @param planDates
     * @param addSchedulePlanReqDTO
     */
    public Boolean createOrUpdatePlanLogs(String[] planDates, AddSchedulePlanReqDTO addSchedulePlanReqDTO) {
        Boolean updateFlag = true;
        SchedulePlanLogs schedulePlanLogs = this.getOne(Wrappers.<SchedulePlanLogs>lambdaQuery().eq(SchedulePlanLogs::getScheduleId, addSchedulePlanReqDTO.getScheduleId()));
        if (schedulePlanLogs == null) {
            schedulePlanLogs = new SchedulePlanLogs();
            updateFlag = false;
        }
        schedulePlanLogs.setPlanStartTime(addSchedulePlanReqDTO.getPlanStartTime());
        schedulePlanLogs.setPlanFinishTime(addSchedulePlanReqDTO.getPlanFinishTime());
        schedulePlanLogs.setPlanRules(addSchedulePlanReqDTO.getPlanRules());
        schedulePlanLogs.setRuleValues(addSchedulePlanReqDTO.getRuleValues());
        schedulePlanLogs.setPlanCounts(planDates.length);
        schedulePlanLogs.setPlanLockTime(Arrays.toString(planDates));

        if (updateFlag) {
            schedulePlanLogs.setUpdateTime(new Date());
            return this.updateById(schedulePlanLogs);
        } else {
            schedulePlanLogs.setCreateTime(new Date());
            schedulePlanLogs.setId(SignleSnowFlake.getSnowflakeId());
            schedulePlanLogs.setScheduleId(addSchedulePlanReqDTO.getScheduleId());
            return this.save(schedulePlanLogs);
        }

    }
}
